package com.liqi.display.utils;

import java.math.BigDecimal;
import java.text.NumberFormat;

/**
 * 计算百分比工具类
 */
public class CalculateUtil {

    /**
     * 计算百分比 整数
     * @param divisor
     * @param dividend
     * @return
     */
    public static String proportionInt(Integer divisor, Integer dividend) {
        if (dividend == null || divisor == null)
            return null;
        if(dividend == 0 && divisor == 0)
            return "0";
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        String result = numberFormat.format((float) divisor / (float) dividend * 100);
        if (result.indexOf(".") != -1) {
            if(result.equals("0")) {
                result = "0%";
            }else {
                result = Math.round(Double.parseDouble(result)) + "%";
            }
            //result = Math.round(Double.parseDouble(result)) + "";
        }else {
            if(result.equals("0")) {
                result = "0%";
            }else {
                result = result + "%";
            }
        }
        return result;
    }
    
    /**
     * 计算百分比 整数
     * @param divisor
     * @param dividend
     * @return
     */
    public static String proportionIntNotRound(Integer divisor, Integer dividend) {
        if (dividend == null || divisor == null)
            return null;
        if(dividend == 0 && divisor == 0)
            return "0";
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        String result = numberFormat.format((float) divisor / (float) dividend * 100);
        if (result.indexOf(".") != -1) {
            if(result.equals("0")) {
                result = "0%";
            }else {
                result = result + "%";
            }
            //result = Math.round(Double.parseDouble(result)) + "";
        }else {
            if(result.equals("0")) {
                result = "0%";
            }else {
                result = result + "%";
            }
        }
        return result;
    }
    
    
    /**
     * 计算评价平均分
     * @param num
     * @param score
     * @return
     */
    public static String scoreDivideNum(Integer num, Integer score) {
        if (num == null || score == null)
            return "0";
        if(num == 0 || score == 0)
            return "0";
        
        double result = new BigDecimal(score).divide(new BigDecimal(num),2,BigDecimal.ROUND_DOWN).doubleValue();
        
        return result+"";
    }
    
    /**
     * 计算评价率
     * @param num
     * @param score
     * @return
     */
    public static String evalRate(Integer total, Integer eval) {
        if (total == null || eval == null)
            return "0.00";
        if(total == 0 || eval == 0)
            return "0.00";
        
        double result = new BigDecimal(eval).divide(new BigDecimal(total),4,BigDecimal.ROUND_DOWN).doubleValue();
        
        return result+"";
    }
    
    
    


    public static void main(String[] args) {
        String anInt = CalculateUtil.scoreDivideNum(3, 10);
        System.out.println(anInt);
        
        System.out.println( new BigDecimal(4033).divide(new BigDecimal(807),2,BigDecimal.ROUND_DOWN).doubleValue());
       
    }
}
